﻿namespace AnyOffice.DAL
{
    using AnyOffice.Components;
    using Microsoft.ApplicationBlocks.Data;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    public class AttendParaDao
    {
        private string _attendManager = string.Empty;
        private int _dutyIntervalAfter1 = 10;
        private int _dutyIntervalAfter2 = 60;
        private int _dutyIntervalBefore1 = 30;
        private int _dutyIntervalBefore2 = 10;
        private string _noDutyUser = string.Empty;

        public void GetPara()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SET NOCOUNT ON ");
            builder.Append("SELECT ");
            builder.Append("\t[ParaId], ");
            builder.Append("\t[ParaName], ");
            builder.Append("\t[ParaValue], ");
            builder.Append("\t[Remark] ");
            builder.Append("FROM ");
            builder.Append("\t[dbo].[Attend_Para] ");
            SqlConnection connection = new SqlConnection(ConfigManager.ConnectionString);
            DataSet set = SqlHelper.ExecuteDataset(connection, CommandType.Text, builder.ToString());
            set.Tables[0].TableName = "Attend_Para";
            DataTable table = set.Tables[0];
            if (table.Rows.Count > 0)
            {
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    string str = table.Rows[i]["ParaName"].ToString();
                    if (str != null)
                    {
                        if (str != "ATTEND_MANAGER")
                        {
                            if (str == "NO_DUTY_USER")
                            {
                                this._noDutyUser = table.Rows[i]["ParaValue"].ToString();
                            }
                            if (str == "DUTY_INTERVAL_BEFORE1")
                            {
                                this._dutyIntervalBefore1 = Convert.ToInt32(table.Rows[i]["ParaValue"].ToString());
                            }
                            if (str == "DUTY_INTERVAL_AFTER1")
                            {
                                this._dutyIntervalAfter1 = Convert.ToInt32(table.Rows[i]["ParaValue"].ToString());
                            }
                            if (str == "DUTY_INTERVAL_BEFORE2")
                            {
                                this._dutyIntervalBefore2 = Convert.ToInt32(table.Rows[i]["ParaValue"].ToString());
                            }
                            if (str == "DUTY_INTERVAL_AFTER2")
                            {
                                this._dutyIntervalAfter2 = Convert.ToInt32(table.Rows[i]["ParaValue"].ToString());
                            }
                        }
                        else
                        {
                            this._attendManager = table.Rows[i]["ParaValue"].ToString();
                        }
                    }
                }
            }
            connection.Close();
        }

        public DataTable SelectAttendManager(SqlConnection conn)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SET NOCOUNT ON ");
            builder.Append("SELECT ");
            builder.Append("\t[UserId], ");
            builder.Append("\t[UserName] ");
            builder.Append("FROM ");
            builder.Append("\t[dbo].[Users] ");
            builder.Append("WHERE ");
            builder.Append("\tCHARINDEX(',' + [UserId] + ',', ',' + CAST(@AttendManager AS nvarchar(2000)) + ',')>0 ");
            builder.Append("ORDER BY ");
            builder.Append("\t[UserNo] ASC ");
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@AttendManager", SqlDbType.NText) };
            commandParameters[0].Value = this._attendManager;
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, builder.ToString(), commandParameters);
            set.Tables[0].TableName = "Users";
            return set.Tables[0];
        }

        public void SetAttendManager(SqlConnection conn)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SET NOCOUNT ON ");
            builder.Append("UPDATE [dbo].[Attend_Para] ");
            builder.Append("SET ");
            builder.Append("\t[ParaValue] = @AttendManager ");
            builder.Append("WHERE ");
            builder.Append("\t[ParaName] = 'ATTEND_MANAGER' ");
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@AttendManager", SqlDbType.NText) };
            commandParameters[0].Value = this._attendManager;
            SqlHelper.ExecuteNonQuery(conn, CommandType.Text, builder.ToString(), commandParameters);
        }

        public void SetInterval(SqlConnection conn)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SET NOCOUNT ON ");
            builder.Append("UPDATE [dbo].[Attend_Para] ");
            builder.Append("SET ");
            builder.Append("\t[ParaValue] = CAST(@DutyIntervalBefore1 as nvarchar(5)) ");
            builder.Append("WHERE ");
            builder.Append("\t[ParaName] = 'DUTY_INTERVAL_BEFORE1' ");
            builder.Append("UPDATE [dbo].[Attend_Para] ");
            builder.Append("SET ");
            builder.Append("\t[ParaValue] = CAST(@DutyIntervalAfter1 as nvarchar(5)) ");
            builder.Append("WHERE ");
            builder.Append("\t[ParaName] = 'DUTY_INTERVAL_AFTER1' ");
            builder.Append("UPDATE [dbo].[Attend_Para] ");
            builder.Append("SET ");
            builder.Append("\t[ParaValue] = CAST(@DutyIntervalBefore2 as nvarchar(5)) ");
            builder.Append("WHERE ");
            builder.Append("\t[ParaName] = 'DUTY_INTERVAL_BEFORE2' ");
            builder.Append("UPDATE [dbo].[Attend_Para] ");
            builder.Append("SET ");
            builder.Append("\t[ParaValue] = CAST(@DutyIntervalAfter2 as nvarchar(5)) ");
            builder.Append("WHERE ");
            builder.Append("\t[ParaName] = 'DUTY_INTERVAL_AFTER2' ");
            SqlParameter[] commandParameters = new SqlParameter[4];
            commandParameters[0] = new SqlParameter("@DutyIntervalBefore1", SqlDbType.Int);
            commandParameters[0].Value = this._dutyIntervalBefore1;
            commandParameters[1] = new SqlParameter("@DutyIntervalAfter1", SqlDbType.Int);
            commandParameters[1].Value = this._dutyIntervalAfter1;
            commandParameters[2] = new SqlParameter("@dutyIntervalBefore2", SqlDbType.Int);
            commandParameters[2].Value = this._dutyIntervalBefore2;
            commandParameters[3] = new SqlParameter("@dutyIntervalAfter2", SqlDbType.Int);
            commandParameters[3].Value = this._dutyIntervalAfter2;
            SqlHelper.ExecuteNonQuery(conn, CommandType.Text, builder.ToString(), commandParameters);
        }

        public void SetNoDutyUser(SqlConnection conn)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SET NOCOUNT ON ");
            builder.Append("UPDATE [dbo].[Attend_Para] ");
            builder.Append("SET ");
            builder.Append("\t[ParaValue] = @NoDutyUser ");
            builder.Append("WHERE ");
            builder.Append("\t[ParaName] = 'NO_DUTY_USER' ");
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@NoDutyUser", SqlDbType.NText) };
            commandParameters[0].Value = this._noDutyUser;
            SqlHelper.ExecuteNonQuery(conn, CommandType.Text, builder.ToString(), commandParameters);
        }

        public string AttendManager
        {
            get
            {
                return this._attendManager;
            }
            set
            {
                this._attendManager = value;
            }
        }

        public int DutyIntervalAfter1
        {
            get
            {
                return this._dutyIntervalAfter1;
            }
            set
            {
                this._dutyIntervalAfter1 = value;
            }
        }

        public int DutyIntervalAfter2
        {
            get
            {
                return this._dutyIntervalAfter2;
            }
            set
            {
                this._dutyIntervalAfter2 = value;
            }
        }

        public int DutyIntervalBefore1
        {
            get
            {
                return this._dutyIntervalBefore1;
            }
            set
            {
                this._dutyIntervalBefore1 = value;
            }
        }

        public int DutyIntervalBefore2
        {
            get
            {
                return this._dutyIntervalBefore2;
            }
            set
            {
                this._dutyIntervalBefore2 = value;
            }
        }

        public string NoDutyUser
        {
            get
            {
                return this._noDutyUser;
            }
            set
            {
                this._noDutyUser = value;
            }
        }
    }
}

